import 'package:flutter/material.dart';
import 'package:iphonexsocialapp/profile_page_view.dart';
import 'package:iphonexsocialapp/values/assets.dart';

import 'app_bottom_bar.dart';
/*
class Homepage extends StatelessWidget {
  final double frictionHeight = 0.75;// 500/797
  @override
  Widget build(BuildContext context) {
    debugPrint(MediaQuery.of(context).size.height.toString());
    return Scaffold(
      body: Stack(
        fit: StackFit.expand,
        children: <Widget>[
          FractionallySizedBox(
            alignment: Alignment.topCenter ,
            heightFactor: frictionHeight,
            child: Image.asset(Assets.GENA_SEDWICK,
              fit:BoxFit.cover,colorBlendMode: BlendMode.hue,color: Colors.black,
            height: 600,),
          ),
          FractionallySizedBox(
            alignment: Alignment.bottomCenter ,
            heightFactor: 1-frictionHeight,
            child: Container(
              color: Colors.teal,
            )
          ),
        ],
      ),
    );
  }
}
*/

class Homepage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<Homepage> with SingleTickerProviderStateMixin {
  AnimationController _controller;
  Animation<double> _heightFactorAnimation;
  bool isAnimationCompleted = false;
  double screenHeight ;
  final double collapsedHeightFactor = 0.90;// 500/797
  final double expandedHeightFactor = 0.60;

  @override
  void initState() {
    _controller = AnimationController(vsync: this,duration: Duration(milliseconds: 400));
    _heightFactorAnimation = Tween<double>(begin: collapsedHeightFactor,end:expandedHeightFactor).animate(_controller);
    _controller.addListener((){
      if(_controller.status == AnimationStatus.completed){
        isAnimationCompleted = true;
      }
    });
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
  onBottomPartTap(){
    setState(() {
      if(isAnimationCompleted){
       // _controller.reverse();
        _controller.fling(velocity: -2);
      }else{
        //_controller.forward();
        _controller.fling(velocity: 2);
      }
      isAnimationCompleted = !isAnimationCompleted;
      debugPrint(isAnimationCompleted.toString());
    });
  }
  Widget getWidget(){
    return Stack(
      fit: StackFit.expand,
      children: <Widget>[
        FractionallySizedBox(
          alignment: Alignment.topCenter ,
          heightFactor: _heightFactorAnimation.value,
          /*
          child: Image.asset(Assets.GENA_SEDWICK,
            fit:BoxFit.cover,colorBlendMode: BlendMode.hue,color: Colors.black,
            height: 600,),
            */
          child: ProfilePageView(),
        ),
        GestureDetector(
          onTap: (){
            onBottomPartTap();
          },
          onVerticalDragUpdate: _handleVerticalUpdate,
          onVerticalDragEnd: _handleVerticalEnd,
          child: FractionallySizedBox(
              alignment: Alignment.bottomCenter ,
              heightFactor: 1.05-_heightFactorAnimation.value,
              child: DecoratedBox(
                decoration: BoxDecoration(
                    color: Color(0xFFEEEEEE),
                    borderRadius: BorderRadius.only(topLeft:Radius.circular(40.0) ,topRight: Radius.circular(40.0))
                ),
                child: Container(

                ),
              )
          ),
        ),
      ],
    );
  }
  _handleVerticalUpdate(DragUpdateDetails details){
    print("update ${details.globalPosition.dy} ${details.primaryDelta} ${details.delta.dy} ctrl-v:${_controller.value}");
    double fractionDragged = details.primaryDelta/screenHeight;
    _controller.value = _controller.value- 5*fractionDragged;
  }
  _handleVerticalEnd(DragEndDetails details){
    if(_controller.value>=0.5){
     // _controller.forward();
      _controller.fling(velocity: 1);
    }else{
      _controller.fling(velocity: -1);
    }
  }
  @override
  Widget build(BuildContext context) {
    screenHeight = MediaQuery.of(context).size.height;
    return Scaffold(
      bottomNavigationBar: AppBottomBar(),
      body: AnimatedBuilder(
        animation: _controller,
        builder: (context,widget){
          return getWidget();
        }
      ),
    );
  }
}
